/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.sport.SportItem;
import ge.info.domain.sport.SportPlace;
import ge.info.domain.sport.SportTournament;
import ge.info.domain.sport.SportType;
import ge.ktapestry.pages.BasePageExt;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;

public abstract class SportTypePage extends BasePageExt implements PageBeginRenderListener
{

    public static int SERVICE_ID = 24;

    public abstract SportItem getSportI();

    public abstract void setSportI(SportItem sportItem);

    public abstract List<SportItem> getSportItem();

    public abstract void setSportItem(List<SportItem> sportItem);

    public abstract SportTournament getSportTournament();

    public abstract void setSportTournament(SportTournament sportTournament);

    public abstract SportPlace getSportPlace();

    public abstract void setSportPlace(SportPlace sportPlace);

    @Persist("session")
    public abstract SportPlace getSportPlaceCurrent();

    public abstract void setSportPlaceCurrent(SportPlace sportPlaceCurrent);

    public abstract int getIndex();

    public abstract void setIndex(int i);

    @Persist("session")
    public abstract SportType getSportType();

    public abstract void setSportType(SportType sportType);

    @Persist("session")
    public abstract SportType getListSportType();

    public abstract void setListSportType(SportType sportType);

    @Persist("session")

    public abstract String getPosterCriteria();

    public abstract void setPosterCriteria(String s);

    @Persist("session")
    
    public abstract Date getScDate();

    public abstract void setScDate(Date date);

    public void pageBeginRender(PageEvent event)
    {


    }

    public void getSport(IRequestCycle cycle, SportPlace st)
    {
        SportPlace sp = new SportPlace();
        sp.setSportPlaceID(st.getSportPlaceID());
        sp.setPlaceName(st.getPlaceName());
        setSportPlaceCurrent(sp);

        SportItem itm = new SportItem();

             itm.setSportPlaceID(st.getSportPlaceID());


         itm.setPosterCriteria(getPosterCriteria());

         if (getScDate()!= null)

         {
            itm.setSdate(getScDate());
         }



        getSportPlaceCurrent().setSportItems(getDbExecutor().searchspPostersdet(itm));


             setScDate(null);
             setPosterCriteria(null);

                //.getSportItems(getSportPlaceCurrent(), getSportType()));

    }

    public void getTournament(SportPlace st)
    {
        SportPlace sp = new SportPlace();
        sp.setSportPlaceID(st.getSportPlaceID());
        sp.setPlaceName(st.getPlaceName());
        setSportPlaceCurrent(sp);
        getSportPlaceCurrent().setSportTournaments(getDbExecutor().getSportTournaments(getSportPlaceCurrent(), getSportType()));

    }

    public void searchPosters(IRequestCycle cycle)
    {
        setSportPlaceCurrent(null);



        if (getPosterCriteria() == null && getScDate() == null)
           
        {
            if (getListSportType().getSportTypeID() != -1)
            {
               setSportType(getDbExecutor().populateSportPlaces(getListSportType()));   
            } else
            {
                setSportType(null);
            }
            return;
        }

        setSportType(new SportType());
        getSportType().setSportTypeID(getListSportType().getSportTypeID());
        getSportType().setPosterCriteria(getPosterCriteria());

        if (getScDate() != null)

         {
            getSportType().setSdate(getScDate());
         }

       

        if (getListSportType().getSportTypeID() != -1)
        {

            setSportType(getDbExecutor().searchspPosters(getSportType()));
        }
        else

        {
           List<SportType> list = getDbExecutor().searchspAllPosters(getSportType());
            SportType sport = new SportType();
            ArrayList<SportPlace> places = new ArrayList<SportPlace>();
            
            sport.setSportPlaces(places);
            for (SportType st : list)                                                                
            {                                                                                      
               for (SportPlace place : st.getSportPlaces())                                
               {                                                                                     
                   places.add(place);                                                               
               }                                                                               
            }                                                                                        
           setSportType(sport);
           
        }
    }
    

    

    public boolean isBreak()
    {
        if (getSportPlaceCurrent() == null || getSportPlaceCurrent().getSportItems() == null || getIndex() == (getSportPlaceCurrent().getSportItems().size() - 1))
        {
            return false;
        }
        return !getSportPlaceCurrent().getSportItems().get(getIndex()).getItemDate().substring(0, 2).equals(getSportPlaceCurrent().getSportItems().get(getIndex() + 1).getItemDate().substring(0, 2));
    }

    public void sendSMS(IRequestCycle cycle, SportItem sport)
    {


        String curDate = sport.getItemDate().substring(0, 2);

        StringBuilder sb = new StringBuilder();
        sb.append(getSportPlaceCurrent().getPlaceName()).append(" ");
        if (sport != null)
        {

            for (SportItem it : getSportPlaceCurrent().getSportItems())
            {
                if (it.getItemDate().substring(0, 2).equals(curDate))
                {

                    //sb.append(it.getPlc()).append(" ");
                    sb.append(it.getItemDate()).append(" ");
                    sb.append(it.getHomesp()).append(" ");
                    sb.append(it.getHomecalcul()).append(" ");
                    sb.append(it.getGuestcalcul()).append(" ");
                    sb.append(it.getGuestsp()).append(" ");
                    //need add components

                }
            }

            getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
        }


    }

    public void clear(IRequestCycle cycle)
    {
       setScDate(null);
       setPosterCriteria(null);
    }


}
